<!-- 2020年2月15日14:57:57 完成 -->
<template>
	<view>
		<view class="edgeInsetTop"></view>
		<wanl-empty v-if="addressList.length === 0 && status !== 'loading'" text="还没添加任何地址" />
		<view class="margin-top-bj">
			<view class="cu-list menu-avatar">
				<view
					class="cu-item1"
					:class="modalName == 'move-box-' + index ? 'move-cur' : ''"
					v-if="addressList"
					v-for="(item, index) in addressList"
					:key="item.id"
					@tap="checkAddress(item)"
					:data-target="'move-box-' + index"
				>
					<view class="content1">
						<view class="name">
							<text class="">{{ item.name }}</text>
							<text class="margin-left">{{ item.mobile }}</text>
						</view>
						<view class="address" style="">{{ item.province }} {{ item.city }} {{ item.district }} {{ item.address }}</view>
					</view>
					<view class="move1">
						<view class="sc" @tap.stop="delAddress(item.id, index)">删除</view>
						<view class="line"></view>
						<view class="bj" @tap.stop="addAddress('edit', item)">编辑</view>
					</view>
				</view>
				<wanl-empty text="没有找到任何地址" src="address_default3x" v-else />
			</view>
		</view>
		<uni-load-more :status="status" :content-text="contentText" />
		<view class="edgeInsetBottom"></view>
		<view class="wanlian cu-bar tabbar foot flex flex-direction"><button @tap="addAddress('add')" class="cu-btn wanl-bg-orange an lg">+ 增加收货地址</button></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			source: 0,
			order_id: 0,
			addressList: [],
			modalName: null,
			listTouchStart: 0,
			listTouchDirection: null,
			//上拉下拉
			total: 0, //数据量
			current_page: 1, //当前页码
			last_page: 1, //总页码
			status: 'loading',
			contentText: {
				contentdown: ' ',
				contentrefresh: '正在加载..',
				contentnomore: ''
			}
		};
	},
	// 监听下拉刷新
	onPullDownRefresh() {
		this.current_page = 1;
		this.reload = true;
		this.loadData();
	},
	// 监听上拉加载
	onReachBottom() {
		//判断是否最后一页
		if (this.current_page >= this.last_page) {
			this.status = 'noMore';
		} else {
			this.current_page = this.current_page + 1; //页码+1
			this.status = 'loading';
			this.loadData(true);
		}
	},
	onLoad(option) {
		this.source = option.source;
		this.order_id = option.order_id;
		this.loadData();
	},
	methods: {
		async loadData(reload, refresh) {
			await uni.request({
				url: '/wanlshop/address/getaddress',
				data: {
					page: reload ? this.current_page : 1
				},
				success: (res) => {
					if (reload) {
						this.addressList = this.addressList.concat(res.data.data);
					} else {
						this.addressList = res.data.data;
						uni.stopPullDownRefresh();
					}
					this.total = res.data.total; //数据量
					this.current_page = res.data.current_page; //当前页码
					this.last_page = res.data.last_page; //总页码
					this.status = res.data.total == 0 ? 'noMore' : 'more';
				}
			});
		},
		// 删除
		async delAddress(id, key) {
			this.$delete(this.addressList, key); //vue方法
			await uni.request({
				url: '/wanlshop/address/deladdress',
				method: 'POST',
				data: { id: id }
			});
		},
		//添加或修改成功之后回调
		async refreshList(data, type) {
			await uni.request({
				url: '/wanlshop/address/address',
				method: 'POST',
				data: { data: data, type: type },
				success: (res) => {
					this.loadData(false, true);
				}
			});
		},
		//选择地址
		async checkAddress(item) {
			if (this.source == 1) {
				this.$wanlshop.prePage().loadData(item.id);
				this.$wanlshop.back(1);
			} else if (this.source == 2) {
				//我的订单修改地址  地址ID   返回订单ID
				this.$wanlshop.prePage().refreshList(item.id, this.order_id);
				this.$wanlshop.back(1);
			} else if (this.source == 3) {
				//添加或修改成功之后回调
				uni.showLoading({
					title: '正在提交新地址'
				});
				// 获取列表
				await uni.request({
					url: '/wanlshop/order/editOrderAddress',
					method: 'POST',
					data: {
						id: this.order_id,
						address_id: item.id
					},
					success: (res) => {
						this.$wanlshop.msg('地址修改成功');
						uni.hideLoading();
						this.$wanlshop.back(1);
					}
				});
			}
		},
		// 新增地址
		addAddress(type, item) {
			this.$wanlshop.to(`/pages/user/address/addressManage?type=${type}&data=${JSON.stringify(item)}`);
		},
		// ListTouch触摸开始
		ListTouchStart(e) {
			this.listTouchStart = e.touches[0].pageX;
		},
		// ListTouch计算方向
		ListTouchMove(e) {
			this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left';
		},
		// ListTouch计算滚动
		ListTouchEnd(e) {
			if (this.listTouchDirection == 'left') {
				this.modalName = e.currentTarget.dataset.target;
			} else {
				this.modalName = null;
			}
			this.listTouchDirection = null;
		}
	}
};
</script>

<style scoped lang="scss">
.cu-list.menu-avatar > .cu-item {
	height: 160rpx;
}

.wanlian.cu-bar.tabbar {
	background-color: transparent;
}
.wanlian.cu-bar.tabbar .cu-btn {
	width: calc(100% - 50rpx);
}
.wanlian.cu-bar.tabbar .cu-btn.lg {
	font-size: 32rpx;
	height: 86rpx;
}
.cu-item1 {
	width: 700rpx;
	background: #ffffff;
	border-radius: 20rpx 20rpx 20rpx 20rpx;
	overflow: hidden;
	margin: 0 auto;
	padding: 32rpx 32rpx 0 32rpx;
	margin-bottom: 20rpx;
	.name {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx;
		color: #000000;
		line-height: 42rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
	}
	.address {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 26rpx;
		color: #979797;
		line-height: 36rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin-bottom: 20rpx;
	}
	.move1 {
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding: 0 10%;
		.sc {
			font-family: Source Han Sans, Source Han Sans;
			font-weight: 400;
			font-size: 24rpx;
			color: #ff0000;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
			text-transform: none;
			padding: 20rpx;
		}
		.line {
			width: 2rpx;
			height: 38rpx;
			background-color: #efefef;
		}
		.bj {
			padding: 20rpx;
			font-family: Source Han Sans, Source Han Sans;
			font-weight: 400;
			font-size: 24rpx;
			color: #000000;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
			text-transform: none;
		}
	}
}
</style>
